package database;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSource;




public class DbcpConnection {
	
	/** print rdf sentences in the form of uri */
	public static void main(String[] args) throws SQLException {
		
		DbcpConnection dbcpConnection = new DbcpConnection();
		
		Connection connection = dbcpConnection.getConnection();
		
		Statement stmt = connection.createStatement();
		PreparedStatement pstmt =  connection.prepareStatement(
									"select uri from uri where id = ?");
		
		ResultSet rs;
		ResultSet rs_s = null;
		ResultSet rs_p = null;
		ResultSet rs_o = null;

		System.out.printf("%-50s%-50s%-50s\n", "s", "p", "o");
		
		rs = stmt.executeQuery("select s, p, o from quadruple limit 30");
		
		while ( rs.next() ) {
			String s = rs.getString("s");
			String p = rs.getString("p").substring(1);
			String o = rs.getString("o");
			
			if ( s.charAt(0) != 'u' )
				System.out.printf("%-50s", s.substring(1));
			else {
				s = s.substring(1);
				pstmt.setString(1, s);
				rs_s = pstmt.executeQuery();
				rs_s.next();
				System.out.printf("%-50s", rs_s.getString("uri"));
			}

			pstmt.setString(1, p);
			rs_p = pstmt.executeQuery();
			rs_p.next();
			System.out.printf("%-50s", rs_p.getString("uri"));
			
			if ( s.charAt(0) != 'u' )
				System.out.printf("%-50s", o.substring(1));
			else {
				o = o.substring(1);
				pstmt.setString(1, o);
				rs_o = pstmt.executeQuery();
				rs_o.next();
				System.out.printf("%-50s", rs_o.getString("uri"));
			}
			
			System.out.println();
		}
		
		stmt.close();
		pstmt.close();
		rs.close();
		if ( rs_s != null )
			rs_s.close();
		if ( rs_p != null )
			rs_p.close();
		if ( rs_o != null)
			rs_o.close();
	}
	
	
	private DataSource dataSource;
	
	private String driverClassName;
	private String url;
	private String usr;
	private String psw;
	private int minIdle = 0;
	private int maxIdle = 0;
	private int maxWait = 0;
	private int initSize = 0;
	private int maxActive = 0;
	
	
	public DbcpConnection() {

		try {
			initDataSource("jdbc:mysql://114.212.190.142:3306/falconet_v05", "dhuang", "dhuang");
//			initDataSource("jdbc:mysql://127.0.0.1/ifttt", "root", "Soviet");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public DbcpConnection(String url, String usr, String psw) {
		try {
			initDataSource(url, usr, psw);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public Connection getConnection() throws SQLException {
		if ( dataSource == null ) {
			initDataSource(url, usr, psw);
		}
		return dataSource.getConnection();
	}
	
	public void initDataSource(String url, String usr, String psw) throws SQLException {

		if ( url.startsWith("jdbc:mysql") )
			driverClassName = "org.gjt.mm.mysql.Driver";
		else if ( url.startsWith("jdbc:db2") )
			driverClassName = "com.ibm.db2.jcc.DB2Driver";//"COM.ibm.db2.jdbc.app.DB2Driver";//
		else throw new SQLException();
		
		this.url = url;
		this.usr = usr;
		this.psw = psw;
		
		initSize = 20;
		minIdle = 5;
		maxIdle = 15;
		maxWait = 3000;
		maxActive = 20;
		
		BasicDataSource bds = new BasicDataSource();
		bds.setDriverClassName(driverClassName);
		bds.setUrl(url);
		bds.setUsername(usr);
		bds.setPassword(psw);
		
		bds.setInitialSize(initSize);
		bds.setMinIdle(minIdle);
		bds.setMaxIdle(maxIdle);
		bds.setMaxWait(maxWait);
		bds.setMaxActive(maxActive);
		
		dataSource = bds;
	}

	@Override
	public String toString() {
		// TODO Auto-generated method stub
		StringBuffer s = new StringBuffer();
		s.append("driverClassName = " + driverClassName + "\n");
		s.append("url = " + url + "\n");
		s.append("usr = " + usr + "\n");
		s.append("psw = " + psw + "\n");
		s.append("minIdle = " + minIdle + "\n");
		s.append("maxIdle = " + maxIdle + "\n");
		s.append("maxWait = " + maxWait + "\n");
		s.append("initSize = " + initSize + "\n");
		s.append("maxActive = " + maxActive + "\n");
		return s.toString();
	}
	
}